package com.multimedia.room;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.multimedia.room.fragment.AudioBarView;
import com.multimedia.room.fragment.HeadBarView;
import com.multimedia.room.media.AudioPlayerManager;
import com.multimedia.room.view.MusicBigAnimationDrawableView;

public class AudioPlayActivity extends Activity implements AudioBarView.IUpdateStatusListenner,
ISwitch,AdapterView.OnItemClickListener {
    private TextView mTitleView, mBottomView;
    private ImageView mThumbView;
    private AudioBarView mAudioBarView;
    private ListView mPlayListView;
    private MusicBigAnimationDrawableView musicBigAnimationDrawableView;
    private Context mContext;
    private static final String TAG = "AudioPlayActivity";

   public static final String KILL_PROCESS_ACTION = "com.multimedia.room.action.KIILL_PLAYER";
    
    private ArrayList<FilesEntity> mAudioList;
    private String mTitle;
    private String mPath;
    private int mIndex;
    private SwitchManager mSwitchManager;
    HeadBarView mHeadBarView;

    TextView mMusicNameView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.audio_player);
        mContext = this.getApplicationContext();
        mSwitchManager = SwitchManager.getInstance();
        mSwitchManager.registerKillPlayerSwitch(this);
        if (getIntent() != null) {
            mAudioList = getIntent().getExtras().getParcelableArrayList("files");
            mPath = getIntent().getExtras().getString("name");
            mTitle = getIntent().getExtras().getString("title");
            mIndex = getIntent().getExtras().getInt("index");
        }
        initView();
        IntentFilter filter =new IntentFilter(KILL_PROCESS_ACTION);
        registerReceiver(mBroadcastReceiver,filter);
    }

    private void initView() {
    	mHeadBarView = (HeadBarView) findViewById(R.id.title_layout);
        mTitleView = (TextView) findViewById(R.id.music_title);
        mMusicNameView = (TextView) findViewById(R.id.music_name);
        mThumbView = (ImageView) findViewById(R.id.thumbnail);
        mAudioBarView = (AudioBarView) findViewById(R.id.playback_controls);
        mAudioBarView.setUpdateStatusListenner(this);
        mPlayListView = (ListView) findViewById(R.id.play_list);
        mPlayListView.setOnItemClickListener(this);
        mBottomView = (TextView) findViewById(R.id.bottom_bar);
        musicBigAnimationDrawableView = (MusicBigAnimationDrawableView) findViewById(R.id.play_icon);
        mPlayListView.setAdapter(new FileAdapter(mAudioList));
        mPlayListView.setSelection(mIndex);
        mAudioBarView.setPath(mPath);
        mTitleView.setText(mTitle);
        mMusicNameView.setText(mTitle);
        
        mHeadBarView.setTitle(CommonUtil.getSeatNo());
        mAudioBarView.setAudioFiles(mAudioList);
        mAudioBarView.setIndex(mIndex);
        mBottomView.setText(mAudioList.size() + "  "+ mContext.getResources().getString(R.string.unit_audio));
    }

    @Override
    public void updateStatus(FilesEntity filesEntity, int index, int status) {
        mTitleView.setText(filesEntity.getName());
        mMusicNameView.setText(filesEntity.getName());
        if (status == AudioPlayerManager.STATE_PLAYING) {
            musicBigAnimationDrawableView.start();
        } else if (status == AudioPlayerManager.STATE_PAUSED) {
            musicBigAnimationDrawableView.pause();
        } else {
            musicBigAnimationDrawableView.start();;
        }
        Log.d(TAG,"updata status " + status);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mAudioBarView.setAudioFiles(mAudioList);
        mAudioBarView.setIndex(position);
        mAudioBarView.setPath(mAudioList.get(position).getUrl());
    }


    class FileAdapter extends BaseAdapter {

        private List<FilesEntity> mFiles = new ArrayList<FilesEntity>();
        private int mIndex = -1;

        public void setSelected(int index) {
            mIndex = index;
            notifyDataSetChanged();
        }

        public FileAdapter(List<FilesEntity> files) {
            mFiles = files;
        }

        @Override
        public int getCount() {
            return mFiles.size();
        }

        @Override
        public Object getItem(int position) {
            return mFiles.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            FilesEntity entity = mFiles.get(position);
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(
                        R.layout.playlist_item_layout, null);
                holder = new ViewHolder();
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.mTitleView = (TextView) convertView.findViewById(R.id.title);
            holder.mTitleView.setText(entity.getName());
            return convertView;

        }

    }

    private static class ViewHolder {
        TextView mTitleView;
        ImageView mIconView;
    }
    
    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){

		@Override
		public void onReceive(Context context, Intent intent) {
			if(intent.getAction().equals(KILL_PROCESS_ACTION)){
				AudioPlayActivity.this.finish();
			}
			
		}
    };

	@Override
	protected void onStart() {
		
		super.onStart();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		unregisterReceiver(mBroadcastReceiver);
		super.onDestroy();
	}    
    
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);

	}

	@Override
	public void switchOn(String params) {
		runOnUiThread(new Runnable(){

			@Override
			public void run() {
				mAudioBarView.realse();
				Log.d(TAG,"finish audio switchOn ");
				AudioPlayActivity.this.finish();
			}
			
		});
		
	}

	@Override
	public void switchOff(String params) {
		// TODO Auto-generated method stub
		
	}

    @Override
    public void broadcastOn(String params) {

    }

    @Override
    public void brocadOff(String params) {

    }

    @Override
	public void onBackPressed() {
		 Log.d(TAG,"finish audio ");
		super.onBackPressed();
	}
	
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		 int keyCode = event.getKeyCode();
		 Log.d(TAG, "keycode :" + event.getKeyCode());
		if(keyCode == KeyEvent.KEYCODE_F6||keyCode == KeyEvent.KEYCODE_F5||keyCode == KeyEvent.KEYCODE_F7||keyCode == KeyEvent.KEYCODE_ENTER){
			return mHeadBarView.dispatchKeyEvent(event);
		}else{
			return super.dispatchKeyEvent(event);
		}
	}
	
}
